package com.qigao.mall.service.impl;

import com.qigao.mall.api.ShopCartService;
import com.qigao.mall.commons.dto.ShopCartCreateDto;
import com.qigao.mall.commons.dto.ShopCartResultDto;
import com.qigao.mall.commons.enums.Result;
import com.qigao.mall.commons.enums.ResultCode;
import com.qigao.mall.jdbc.mapper.TbItemMapper;
import com.qigao.mall.jdbc.mapper.TbItemSkuMapper;
import com.qigao.mall.jdbc.mapper.TbShopCartMapper;
import com.qigao.mall.jdbc.model.TbItem;
import com.qigao.mall.jdbc.model.TbItemSku;
import com.qigao.mall.jdbc.model.TbShopCart;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author luiz
 * @Title: com.qigao.mall.service.impl
 * @ProjectName three-mall
 * @date 2019-11-2019/11/4 17:01
 * @Description:
 */
@Service
public class ShopCartServiceImpl implements ShopCartService {
    private static final Logger logger= LoggerFactory.getLogger(ShopCartServiceImpl.class);
    @Resource
    private TbShopCartMapper tbShopCartMapper;
    @Resource
    private TbItemMapper tbItemMapper;
    @Resource
    private TbItemSkuMapper tbItemSkuMapper;
    @Override
    public Result addShoppingCart(ShopCartCreateDto shopCartCreateDto) {
        TbShopCart tbShopCart=tbShopCartMapper.selectByIndex(shopCartCreateDto.getBuylerId(),shopCartCreateDto.getItemNo());
        //获取商品单价
        TbItem tbItem=tbItemMapper.selectByItemNo(shopCartCreateDto.getItemNo());
        if(tbItem==null){
            logger.info("商品信息不存在:{}",shopCartCreateDto.getItemNo());
            return Result.newError(ResultCode.ITEM_NOT_EXITS);
        }
        TbItemSku tbItemSku =tbItemSkuMapper.selectByStockNo(shopCartCreateDto.getStockNo());
        if(tbItemSku==null){
            logger.info("库存信息不存在:{}",shopCartCreateDto.getStockNo());
            return Result.newError(ResultCode.STOCK_NOT_EXITS);
        }
        if(tbShopCart!=null){
            tbShopCart.setNum(shopCartCreateDto.getBuyNum());
            tbShopCart.setPrice(tbItemSku.getPrice());
            tbShopCart.setModiTime(new Date());
            tbShopCartMapper.addShopCart(tbShopCart);
        }else{
            tbShopCart=new TbShopCart();
            tbShopCart.setUserNo(shopCartCreateDto.getBuylerId());
            tbShopCart.setItemNo(shopCartCreateDto.getItemNo());
            tbShopCart.setNum(shopCartCreateDto.getBuyNum());
            tbShopCart.setPrice(tbItemSku.getPrice());
            tbShopCart.setCreateTime(new Date());
            tbShopCart.setModiTime(new Date());
            tbShopCartMapper.insertSelective(tbShopCart);
        }
        logger.info("{}的购物车添加商品:{}成功",shopCartCreateDto.getBuylerId(),shopCartCreateDto.getItemNo());
        return Result.newSuccess();
    }

    @Override
    public List<ShopCartResultDto> findShopCartByUserNo(String currentUserNo) {
        List<TbShopCart> tbShopCarts=tbShopCartMapper.selectByUserNo(currentUserNo);
        if(tbShopCarts==null)    return null;
        List<ShopCartResultDto> shopCartResultDtos=new ArrayList<>();
        for(TbShopCart tbShopCart:tbShopCarts){
            //获取商品单价
            TbItemSku tbItemSku =tbItemSkuMapper.selectByStockNo(tbShopCart.getStockNo());
            ShopCartResultDto shopCartResultDto=new ShopCartResultDto();
            shopCartResultDto.setBuylerId(tbShopCart.getUserNo());
            shopCartResultDto.setBuyNum(tbShopCart.getNum());
            shopCartResultDto.setPrice(tbShopCart.getPrice());
            shopCartResultDto.setItemNo(tbShopCart.getItemNo());
            shopCartResultDto.setItemTitle(tbItemSku.getStockTitle());
            //获取当前最新价格
            shopCartResultDto.setCurrentPrice(tbItemSku.getPrice());
            shopCartResultDto.setPicUrl(tbItemSku.getPicUrl());
            shopCartResultDtos.add(shopCartResultDto);
        }
        return shopCartResultDtos;
    }
}
